package com.ruoyi.recycle.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ruoyi.common.core.web.domain.AjaxResult;
import com.ruoyi.recycle.domain.AdminAddress;
import com.ruoyi.recycle.domain.AdminAddressConfig;
import com.ruoyi.recycle.dto.AdminAddressDto;
import com.ruoyi.recycle.mapper.AdminAddressConfigMapper;
import com.ruoyi.recycle.mapper.AdminAddressMapper;
import com.ruoyi.recycle.service.AdminAddressService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class AdminAddressImpl extends ServiceImpl<AdminAddressMapper, AdminAddress> implements AdminAddressService {

    @Resource
    private AdminAddressMapper adminAddressMapper;
    @Resource
    private AdminAddressConfigMapper adminAddressConfigMapper;

    @Override
    public AjaxResult updateAdminAddress(AdminAddressDto adminAddressDto) {

        AdminAddress adminAddress = adminAddressDto.getAdminAddress();
        if (ObjectUtils.isEmpty(adminAddress.getId())){
            adminAddressMapper.insert(adminAddress);
        }else {
            adminAddressMapper.updateById(adminAddress);
        }
        List<AdminAddressConfig> adminAddressConfigs = adminAddressDto.getAdminAddressConfigs();
        adminAddressConfigMapper.delete(new LambdaQueryWrapper<AdminAddressConfig>().eq(AdminAddressConfig::getAdminAddressId,adminAddress.getId().longValue()));
        if (adminAddressConfigs.size()>0){
            for (AdminAddressConfig adminAddressConfig : adminAddressConfigs) {
                adminAddressConfig.setAdminAddressId(adminAddress.getId().longValue());
                adminAddressConfigMapper.insert(adminAddressConfig);
            }
        }
        return AjaxResult.success();
    }


    @Override
    public AjaxResult setExpress(String express,Long addressId) {
        AdminAddress adminAddress = adminAddressMapper.selectById(addressId);

        if (ObjectUtils.isEmpty(adminAddress)){
            return AjaxResult.error("请先设置收货地址");
        }
        adminAddress.setKuaidi(express);
        adminAddressMapper.updateById(adminAddress);

        return AjaxResult.success();
    }

    @Override
    public AjaxResult getExpress() {

        String defaultCode = adminAddressMapper.selectDefaultCode();

        List<Map<String,String>> expressList = adminAddressMapper.selectExpressList();

        return AjaxResult.success("defaultCode",defaultCode).put("expressList",expressList);
    }

    @Override
    public AjaxResult getAdminAddress() {
        List<AdminAddress> adminAddresses = adminAddressMapper.selectList(new LambdaQueryWrapper<>());
        return AjaxResult.success(adminAddresses);
    }

    @Override
    public void removeAdminAddress(Long id) {
        this.removeById(id);
        adminAddressConfigMapper.delete(new LambdaQueryWrapper<AdminAddressConfig>().eq(AdminAddressConfig::getAdminAddressId,id));
    }

}
